package com.woniuxy.action;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.omg.PortableInterceptor.SUCCESSFUL;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.woniuxy.entity.User;
import com.woniuxy.service.UserService;

public class LoginAction extends ActionSupport implements ModelDriven<User>,RequestAware,SessionAware{
	private User user;
	private Map<String,Object> request;
	private Map<String,Object> session;
	private UserService service;
	
	public String login(){
		System.out.println(user.getUserName());
		if(service.isLogin(user.getUserName(), user.getPassWord())){
			User u = service.findUser(user.getUserName());
			session.put("user", u);
			return SUCCESS;			
		}
		request.put("result", "登录失败");
		return ERROR;
	}
	
	public void validateLogin() {
		// TODO Auto-generated method stub
	
		if(StringUtils.isBlank(user.getUserName())){
			System.out.println("#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@##############");
			addFieldError("name", "用户名不能为空");
		}
	}

	public User getModel() {
		// TODO Auto-generated method stub
		this.user = new User();
		this.service = new UserService();
		return user;
	}

	public void setSession(Map<String, Object> session) {
		// TODO Auto-generated method stub
		this.session = session;
	}

	public void setRequest(Map<String, Object> request) {
		// TODO Auto-generated method stub
		this.request = request;
	}
	
}
